package com.msi.visualization;

import java.util.List;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

import com.msi.Vertex;
import com.msi.algorithm.CSPAlgorithm;
import com.msi.algorithm.Table;

public class MsiColorLImpl extends MsiColor {
    CSPAlgorithm alg;

    public MsiColorLImpl() {
        boolean rootIsConfigured = Logger.getRootLogger().getAllAppenders()
                .hasMoreElements();
        if (!rootIsConfigured)
            BasicConfigurator.configure();
        graph = GraphVisualization.getInstance().getGraph();
    }

    public void color() {
        long start = System.currentTimeMillis();
        alg = new CSPAlgorithm(graph);
        List<Table> tables = alg.runAlgorithm();
        setColors(tables);
        long stop = System.currentTimeMillis();
        ;
        computationTime = stop - start;
    }

    private void setColors(List<Table> tables) {
        int colorValue = -1;
        for (Table t : tables) {
            ++colorValue;
            for (Vertex v : t.getListOfSittingGuests()) {
                graph.getVertex(v.getId()).setColor(colorValue);
            }
        }
        numberOfColors = colorValue + 1;
    }
}
